package com.cz.ui.mine;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.cz.R;
import com.cz.utils.DBOpenHelper;

import java.lang.reflect.Field;

public class MineList extends Fragment {

    private DBOpenHelper dbOpenHelper;
    private ImageView avatar;
    private TextView nickname;
    private TextView userid;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_mine_list, container, false);

        avatar = root.findViewById(R.id.img_head);
        nickname = root.findViewById(R.id.tv_user_name);
        userid = root.findViewById(R.id.tv_user_passName);

        return root;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        selectDatabase();
    }

    /**
     * 数据库查询
     */
    private void selectDatabase() {
        dbOpenHelper = new DBOpenHelper(getActivity(),"WeChatMini",null,2);
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();

        //满足条件查询
        Cursor user = db.query("mine", null, "userid=?",
                new String[]{"cz"}, null, null, null);

        R.drawable drawable = new R.drawable();
        int resId=0;
        Field field= null;

        while(user.moveToNext()) {
            nickname.setText(user.getString(2));
            userid.setText("微信号 : " + user.getString(0));//拼接

            try {
                field = R.drawable.class.getField(user.getString(4));
                resId=(Integer)field.get(drawable);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            avatar.setImageResource(resId);

        }
        db.close();
    }
}
